package service

import (
	"fmt"
	//"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/tycme/gin-chat/models"
	"net/http"
)

func FindUserListByRoomID(c *gin.Context) {
	RoomId := c.Query("room_id")
	if RoomId == "" {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "房间名不能为空",
		})
		return
	}
	prefixAccount := c.Query("prefixAccount")

	// 判断用户是否属于该房间prefixAccount
	//uc := c.MustGet("user_claims").(*helper.UserClaims)
	//_, err := models.GetUserRoomByUserIdRoomId(uc.Identity, RoomId)
	//if err != nil {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": -1,
	//		"msg":  "非法访问",
	//	})
	//	return
	//}
	//pageIndex, _ := strconv.ParseInt(c.Query("page_index"), 10, 32)
	//pageSize, _ := strconv.ParseInt(c.Query("page_size"), 10, 32)
	//skip := (pageIndex - 1) * pageSize
	data, err := models.FindUserListByRoomID(RoomId, prefixAccount)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "系统异常" + err.Error(),
		})
		return
	}

	Manager.Clients.Range(func(key, value interface{}) bool {
		data := value.(*Client)
		fmt.Println(key)
		fmt.Println(data.Account + "    " + data.UserId)
		return true
	})
	for _, value := range data {
		_, ok := Manager.Clients.Load(value.UserId)
		if value.UserId == "37a954124f6c4831a5270654877eb7d9" {
			fmt.Println(value.UserId + " =====" + value.Account)
		}
		//fmt.Println(value.UserId + " " + value.Account)
		if ok {
			value.Status = "1"
		} else {
			value.Status = "0"
		}
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "数据加载成功",
		"data": gin.H{
			"list": data,
		},
	})
}
